using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Plugin.QQInfo.Scene
{
	public class NetSceneQQInfo : NetSceneBaseEx<GetQQGroupRequest, GetQQGroupResponse, GetQQGroupRequest.Builder>
	{
		public enum OPTYPE : uint
		{
			OPTYPE_GROUP,
			OPTYPE_FRIEND
		}
		private const string TAG = "NetSceneQQInfo";
		public const int MM_STATUS_WEIXIN_OFF = 0;
		public const int MM_STATUS_WEIXIN_ON = 1;
		public const int MM_STATUS_WEXIN_FRIEND = 2;
		private List<Contact> qqContactList;
		public bool doScene(NetSceneQQInfo.OPTYPE opType, uint groupID)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.OpType = (uint)opType;
			this.mBuilder.GroupID = groupID;
			this.mSessionPack.mCmdID = 38;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(GetQQGroupRequest request, GetQQGroupResponse response)
		{
			if (request.OpType == 0u)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_QQGROUP_ERR, null, null);
				return;
			}
			if (request.OpType == 1u)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_QQFRIEND_ERR, null, null);
			}
		}
		protected override void onSuccess(GetQQGroupRequest request, GetQQGroupResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				if (request.OpType == 0u)
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_QQGROUP_ERR, null, null);
					return;
				}
				if (request.OpType == 1u)
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_QQFRIEND_ERR, null, null);
				}
				return;
			}
			else
			{
				if (request.OpType != 0u)
				{
					if (request.OpType == 1u)
					{
						if (!this.saveQQFriendInfo(request, response))
						{
							EventCenter.postEvent(EventConst.ON_NETSCENE_QQFRIEND_ERR, null, null);
							Log.smethod_5("NetSceneQQInfo", "QQInfo failed");
							return;
						}
						EventCenter.postEvent(EventConst.ON_NETSCENE_QQFRIEND_SUCCESS, null, null);
						StorageMgr.contact.updateList(this.qqContactList);
					}
					return;
				}
				if (!this.saveQQGroupInfo(request, response))
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_QQGROUP_ERR, null, null);
					Log.smethod_5("NetSceneQQInfo", "QQInfo failed");
					return;
				}
				EventCenter.postEvent(EventConst.ON_NETSCENE_QQGROUP_SUCCESS, null, null);
				return;
			}
		}
		private bool saveQQGroupInfo(GetQQGroupRequest request, GetQQGroupResponse response)
		{
			List<QGroup> list = new List<QGroup>();
			using (IEnumerator<QQGroup> enumerator = response.QQGroup.QQGroupsList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					QQGroup current = enumerator.get_Current();
					if (current.MemberNum == 0u)
					{
						StorageMgr.qgroup.method_7(current.GroupID);
					}
					else
					{
						QGroup qGroup = new QGroup();
						qGroup.nGroupID = current.GroupID;
						qGroup.nWeixinNum = (int)current.WeixinNum;
						qGroup.strGroupName = current.GroupName;
						qGroup.strMD5 = current.MD5;
						qGroup.nMemberNum = (int)current.MemberNum;
						list.Add(qGroup);
						Log.smethod_5("NetSceneQQInfo", string.Concat(new object[]
						{
							"qqGroup.nGroupID = ",
							qGroup.nGroupID,
							" strGroupName = ",
							qGroup.strGroupName,
							" nMemberNum = ",
							qGroup.nMemberNum
						}));
					}
				}
			}
			StorageMgr.qgroup.updateList(list);
			return true;
		}
		private bool saveQQFriendInfo(GetQQGroupRequest request, GetQQGroupResponse response)
		{
			List<QFriend> list = new List<QFriend>();
			if (this.qqContactList == null)
			{
				this.qqContactList = new List<Contact>();
			}
			else
			{
				this.qqContactList.Clear();
			}
			Log.smethod_5("NetSceneQQInfo", string.Concat(new object[]
			{
				"response.QQFriend.GroupID = ",
				response.QQFriend.GroupID,
				" nMemberNum = ",
				response.QQFriend.QQFriendsList.get_Count()
			}));
			uint groupID = response.QQFriend.GroupID;
			IEnumerable<string> enumerable = Enumerable.Select<QQFriendInGroup, string>(Enumerable.Where<QQFriendInGroup>(response.QQFriend.QQFriendsList, (QQFriendInGroup qqfriendInGroup_0) => qqfriendInGroup_0.WeixinStatus != 0u), (QQFriendInGroup qqfriendInGroup_0) => qqfriendInGroup_0.UserName);
			List<string> userNameList = null;
			if (Enumerable.Count<string>(enumerable) > 0)
			{
				userNameList = Enumerable.ToList<string>(enumerable);
			}
			Dictionary<string, Contact> dictionary = StorageMgr.contact.getDictionary(userNameList);
			using (IEnumerator<QQFriendInGroup> enumerator = response.QQFriend.QQFriendsList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					QQFriendInGroup current = enumerator.get_Current();
					QFriend qFriend = new QFriend();
					qFriend.nQQ = current.QQUin;
					qFriend.nGroupID = (int)groupID;
					qFriend.nWeixinStatus = current.WeixinStatus;
					if (current.WeixinStatus != 0u)
					{
						if (current.UserName == null || current.UserName.Equals(""))
						{
							continue;
						}
						if (dictionary == null)
						{
							qFriend.nWeixinStatus = 1u;
						}
						else
						{
							if (dictionary.ContainsKey(current.UserName))
							{
								Contact contact = dictionary.get_Item(current.UserName);
								if (contact != null && contact.nQQ == 0u)
								{
									contact.nQQ = current.QQUin;
									this.qqContactList.Add(contact);
								}
								if (contact != null && contact.strUsrName.Equals(current.UserName) && ContactMgr.isContact(contact))
								{
									qFriend.nWeixinStatus = 2u;
								}
								else
								{
									qFriend.nWeixinStatus = 1u;
								}
							}
							else
							{
								qFriend.nWeixinStatus = 1u;
							}
						}
					}
					qFriend.strUsrName = current.UserName;
					qFriend.strNickName = current.NickName;
					qFriend.strQQRemarkName = current.QQRemarkName;
					qFriend.strQQNickName = current.QQNickName;
					qFriend.strPYInitial = SortStringsByAlpha.ConvertStringToPinyinInitial(current.NickName);
					qFriend.strQuanPin = GetPinYin.ConvertStrToQuanPin(current.NickName);
					qFriend.strQQPYInitial = SortStringsByAlpha.ConvertStringToPinyinInitial(current.QQNickName);
					qFriend.strQQQuanPin = GetPinYin.ConvertStrToQuanPin(current.QQNickName);
					qFriend.strQQRemarkPYInitial = SortStringsByAlpha.ConvertStringToPinyinInitial(current.QQRemarkName);
					qFriend.strQQRemarkQuanPin = GetPinYin.ConvertStrToQuanPin(current.QQRemarkName);
					qFriend.nSex = current.Sex;
					qFriend.nPersonalCard = (int)current.PersonalCard;
					qFriend.strProvince = current.Province;
					qFriend.strCity = current.City;
					qFriend.strSignature = current.Signature;
					qFriend.strAlias = current.Alias;
					qFriend.nSnsFlag = current.SnsUserInfo.SnsFlag;
					qFriend.strSnsBGImgID = current.SnsUserInfo.SnsBGImgID;
					qFriend.nSnsBGObjectID = current.SnsUserInfo.SnsBGObjectID;
					list.Add(qFriend);
				}
			}
			StorageMgr.qfriend.updateList(list);
			return true;
		}
	}
}
